package com.huizhonglm.diap.ui.app;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.app.FeedbackParams;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.ui.BaseActivity;


/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2015-08-20 15:32
 */
public class FeedbackActivity extends BaseActivity implements View.OnClickListener {

    private EditText contentEdt,contactEdt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        setBackEvent(this, null);
        assignViews();
    }

    @Override
    protected void assignViews() {
        contentEdt = (EditText) findViewById(R.id.content_edt);
        contactEdt = (EditText) findViewById(R.id.contact_edt);
        View submitBtn = findViewById(R.id.submit_btn);

        submitBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submit_btn:
                feedback();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void feedback() {
        String content = contentEdt.getText().toString().trim();
        String contact = contactEdt.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.makeTextShort("请输入反馈内容");
            return;
        }
        if (TextUtils.isEmpty(contact)) {
            ToastUtil.makeTextShort("请输入联系方式");
            return;
        }
        showProgressBar();
        FeedbackParams params = new FeedbackParams();
        if (UserUtil.isLogin()) {
            params.userId = UserUtil.getUserId();
        }
        params.content = content;
        params.contactMethod = contact;
        API.http().post("feedback", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())){
                        hideProgressBar();
                        ToastUtil.makeTextShort("反馈成功");
                        onBackPressed();
                    } else {
                        hideProgressBar();
                        ToastUtil.makeTextShort("反馈失败，请稍候重试");
                    }
                } else {
                    hideProgressBar();
                    ToastUtil.makeTextShort("反馈失败，请稍候重试");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                hideProgressBar();
                ToastUtil.makeTextShort("反馈失败，请稍候重试");
            }
        });
    }
}
